package traversal;

import java.util.Stack;

public class Traversing {
	Tree tree;
	BNode root;
	BNode current;
	Stack<BNode> stack;
	public static void main(String [] args){
		new Traversing().run();
		
	}
	
	public void run(){
		tree = new Tree(); // Maakt de tree.
		root = tree.getRoot();
		stack = new Stack<BNode>();
		
		printRoom();			// Maakt wat ruimt in de console. (ter onderscheiding)
		inOrder();
		
		printRoom();
		preOrder();
		
		printRoom();
		postOrder();
	}
	
	public void inOrder(){
		current = root; // Begin bij de root
		System.out.println("in order");
		while ( current != null || ! stack.isEmpty()){
			//System.out.println("Node contains " + current.getInhoud());
			if ( current != null){
					stack.push(current);
					current = current.leftBNode;
			} 
			else {
				current = stack.pop();
				System.out.println("Node contains " + current.getInhoud());
				current = current.rightBNode;
			}
		}

	}
	
	public void preOrder(){
		System.out.println("pre order");
		current = root; // Begin bij de root
		while ( current != null || ! stack.isEmpty() ) {
			if ( current != null ) {	
			
				System.out.println("Node contains " + current.getInhoud());
				stack.push(current);
				current = current.leftBNode;
			} 
			else {				
				current = stack.pop();		
				current = current.rightBNode;	
			}
		}

	}
	
	public void postOrder(){
		System.out.println("post order");
		current = root; // Begin bij de root
		while (current != null || ! stack.isEmpty()){
			if ( current != null ){
				stack.push(current);
				current = current.leftBNode;
			} 
			else {
				current = stack.pop();
				if ( current.secondPop){
					System.out.println( "Node contains " + current.getInhoud());
					current = null;			
				} 
				else {			
					current.secondPop = true;
					stack.push(current);
					current = current.rightBNode;		
				}
			}
		}

	}
	
	
	public void printRoom(){
		for(int i=0;i<5;i++){
			System.out.println("_________________________________________________________________");
		}
	}
}

